#!/usr/bin/perl -w

package clreqd_modules;

use strict;
use lvm;
use clreq;
use clreqdxml;
use Sys::Hostname;

#########################################################################
# This is the code that allows a node to attempt to connect to		#
# another specified node - it is typically used when testing 		#
# testing connectivity to new nodes to add to the cluster.		#
#########################################################################

sub echo_to {
my %ARGS=@_;

	my $NODE=$ARGS{NODE};
	if($NODE eq hostname) {
		return "Alive!";
	}
	#################################################################
	# Load in clreqdxml object, aborting if a problem.		#
	#################################################################
	my $CLREQDXML=new clreqdxml("$::ENV{TRUECL_CFG}/clreqd.xml");
	if(!defined($CLREQDXML)) {
		return "ERROR - Unable to load Request Daemon configuration info.";
	};
	my $node_conn=new clreq(hosts => [$NODE], clreqdxml => $CLREQDXML, logger => $::LOGGER, timeout => 10);

	if(! defined($node_conn)) {
		return "ERROR - Unable to create object - is it a known host?";
	}
	my $res=$node_conn->run(cmd => ["echo"]);
	if(!defined($res)) {
		return "ERROR Could not connect to node '$NODE'.";
	} else {
		for (keys %$res) {
			if($res->{$_} !~ /^Alive/) {
				return "ERROR Response from '$NODE' was '$res->{$_}'.";
			}
		}
	}
	return "Alive!";
}

1;
